package com.example.fresh_fruit.controller;

import com.example.fresh_fruit.entity.Commodity;
import com.example.fresh_fruit.permission.Permission;
import com.example.fresh_fruit.service.CommodityService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings({"all"})
/**
 * @author :飞天の鱼
 * @date : 2022/8/15 15:04
 */
@Slf4j
@RestController
@RequestMapping("/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;


    @PostMapping("/insert")
    @ApiOperation(value = "注册")
    public boolean insert(@RequestBody Commodity commodity) {
        Commodity commodity1 = commodityService.findName(commodity.getName());//查询到数据库的
        String newName = commodity.getName();//前端请求传来的名称
        if (commodity1 != null) {
            System.out.println("用户名已存在");
            return false;
        } else {
            System.out.println("插入成功");
            return commodityService.insertCom(commodity);
        }
    }

    //修改商品的状态，判断商品是否被删除

    @PutMapping("/upCommodity")
//    @Transactional
    @Permission(100)
    public boolean upstate(@RequestBody Commodity commodity) {
        boolean result = true;
        Commodity commodity1 = commodityService.isdelete(commodity.getName());
        if (commodity1.getIsdelete() != 1) {
            result = false;
            System.out.println("商品已被删除,无法修改");
        } else {
            result = commodityService.upCom(commodity);
            System.out.println("商品状态已修改");
        }
        return result;
    }

    @PostMapping("/isdelete")
    public Commodity isdelete(@RequestBody String name) {
        return commodityService.isdelete(name);
    }

    @PutMapping("/updata")
    public boolean updata(@RequestBody Commodity commodity) {
        return commodityService.updata(commodity);
    }

    @PostMapping("/findBuName")
    public List<Commodity> findByName(@RequestBody String name) {
        return commodityService.findByName(name);
    }

    @GetMapping("/findById")
    public Commodity findById(@RequestBody Integer id) {
        return commodityService.findById(id);
    }
}
